package com.lx.thread.MyThread.lx;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread1 {
    public volatile static List<Integer>list= Collections.synchronizedList( new ArrayList<>());

    public static ReentrantLock reentrantLock=new ReentrantLock();

    public static Object object =new Object();

    public volatile static Integer count=0;
    CyclicBarrier cyclicBarrier = new CyclicBarrier(10,()->{
        System.out.println("insert");
        System.out.println("insert method Thread name ="+Thread.currentThread().getName());
        System.out.println("insert method listSize="+list.size());
        reloadCountDownLatch();
        count=0;
    });



    public static void main(String[] args) throws InterruptedException {
        MyThread1 myThread1 = new MyThread1();
        for (int i = 0; i <25; i++) {
            new Thread(()->{
               myThread1.add();
            }).start();

        }
    }
    public void addData(Integer object){
        while (count!=10){
            ++count;
            list.add(object);
        }

    }

    public void add(){
        Random random = new Random(100);
        addData(random.nextInt());
        System.out.println("list.size="+list.size());
        System.out.println("thread name is "+Thread.currentThread().getName());
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }


    public synchronized static void reloadCountDownLatch(){
        list.clear();
    }


    /**
     * <p>
     *
     * </p>
     * @param  list
     * @return void
     * @Author zlx
     * @date 2021/4/18
     */
    public  static void outSize(List list){
        System.out.println("list size print " +list.size());
    }




}